from datetime import timedelta


def split_day(start_date, end_date):
    # 按自然天划分并生成时间区间
    date_ranges = []
    current_date = start_date

    # 第一段保留原始开始时间到当天的结束（次日零点）
    first_day_end = (current_date + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
    date_ranges.append((current_date, min(first_day_end, end_date)))

    # 从第二天开始按自然天划分
    current_date = first_day_end

    while current_date < end_date:
        # 次日的 00:00:00
        next_date = current_date + timedelta(days=1)
        # 每天的区间
        date_ranges.append((current_date, min(next_date, end_date)))
        current_date = next_date
    return date_ranges

# 测试
# 定义开始时间和结束时间
# start = '2023-10-01 08:00:00'
# end = '2023-10-05 08:00:00'
# start_date = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
# end_date = datetime.strptime(end, "%Y-%m-%d %H:%M:%S")
# # 打印划分的每日时间段
# for start, end in split_day(start_date, end_date):
#     print(f"开始时间: {start}, 结束时间: {end}")
